package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.UserContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/revise")
public class UpdateController {
    @Autowired
    private IEmployeeService employeeService;


    @RequestMapping("/update")
    public String update(){
        return "custom/updatePwd";
    }

    @RequestMapping("/change")
    @ResponseBody
    public JsonResult change(String oldPassword, String newPassword) {
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee)subject.getPrincipal();
        Md5Hash md5Hash = new Md5Hash(newPassword, employee.getName(), 1);
        employee.setPassword(md5Hash.toString());
        Md5Hash md5Hash1 = new Md5Hash(oldPassword, employee.getName(), 1);
        try {
            if (oldPassword!=null&&newPassword!=null){

                //调用实现方法
                employeeService.updateById(oldPassword,newPassword,employee.getId());
            }
            return new JsonResult(true,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }
}
